package com.heima.tess4j;

import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;

import java.io.File;

/**
 * ClassName: Application
 * Package: com.heima.tess4j
 * Description:
 *
 * @Author 周阳阳
 * @Create 2024/10/18 10:56
 * @Version 1.0
 */
public class Application {
    /**
     * 识别图片中的文字
     * @param args
     */
    public static void main(String[] args){
        try {
            //创建实例
            ITesseract tesseract = new Tesseract();
            //设置字体库路径
            tesseract.setDatapath("D:\\javacode\\heima-leadnews\\tessdata");
            //设置语言 --->简体中文
            tesseract.setLanguage("chi_sim");
            File file = new File("D:\\javacode\\heima-leadnews\\143.png");
            //识别图片
            String result = null;
            result = tesseract.doOCR(file);
            System.out.println("识别的结果为：" + result.replaceAll("\\r|\\n","-"));
        } catch (TesseractException e) {
            System.err.println("Tesseract OCR 错误: " + e.getMessage());
            e.printStackTrace();
        }
    }
}
